using B.S.BaseData.Domain.Entities.RBAC;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.BaseData.WriteAPI.Applications.Commands.Users;
using B.S.BaseData.WriteAPI.Dtos.Users;
using B.S.BaseData.WriteAPI.Utils;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace B.S.BaseData.WriteAPI.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = "Users")]
    [Authorize]
    public class UserController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<UserController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [AllowAnonymous]
        [LogOperation("用户管理", "新增", "添加新用户")]
        public async Task<ActionResult<ApiResult>> AddUser(AddUserDto user)
        {
            try
            {
                var command = new AddUserCommand { user = user };
                var result = await _mediator.Send(command);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "新增用户信息失败");
                return StatusCode(500, ApiResult.Fail("新增用户失败", StatusCodeEnum.Exception));
            }
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="dto">用户信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        [LogOperation("用户管理", "修改", "更新用户信息")]
        public async Task<ActionResult<ApiResult>> UpdateUser([FromBody] UpdateUserDto dto)
        {
            try
            {
                _logger.LogInformation($"收到修改用户信息请求，UserId: {dto.UserId}");
                var command = new UpdateUserCommand { UserDto = dto };
                var result = await _mediator.Send(command);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改用户信息失败");
                return StatusCode(500, ApiResult.Fail("修改用户信息失败", StatusCodeEnum.Exception));
            }
        }
        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="dto">用户信息</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        [LogOperation("用户管理", "删除", "删除用户")]
        public async Task<ApiResult> DeleteUser(int UserId)
        {
            try
            {
                var command = new DeleteUserCommand { UserId = UserId };
                _logger.LogInformation($"收到删除用户信息请求，UserId: {command.UserId}");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除用户信息失败");
                return ApiResult.Fail("删除用户信息失败", StatusCodeEnum.Exception);
            }
        }




    }
}